SUBROUTINE OLS
USE SHARE
USE LINDS_INT
IMPLICIT NONE
DOUBLE PRECISION :: PV,SU,SUMZ,TSA,SR,SUMR,SSTA
INTEGER :: LA,MA,IA,IC,JZ,JY,IW,I,ib,lb
      DO 201 LA = 1,REGN                                                   
!         PRINT 2016, LA,(X(LA,ic),ic=1,REGK); 2016 format (' LA ',i2,2(2x,f12.6))
      DO 202 MA = 1,REGK                                                   
      XTR(MA,LA) = X(LA,MA)                                             
 202  CONTINUE                                                          
 201  CONTINUE                                                          
      DO 205 IA = 1,REGK                                                   
      DO 203 IC = 1,REGK
      SU=0.0D0                                                    
      DO 204 IB = 1,REGN
      SU=SU+((XTR(IA,IB)) * (X(IB,IC)))                         
 204  CONTINUE                                                          
      XTRX(IA,IC)=SU                                                
 203  CONTINUE                                                          
 205  CONTINUE                                                          
      CALL LINDS (XTRX,XTRXI,N=REGK,LDA=2,LDAINV=2)
!
!     PRINT 2056, N,K; 2056 format (' After call to LINDS N = ',i4,' K = ',i4)
! NB  The variables in the argument list are local to the subroutine.
!     So for example setting N=K in the argument list of this call tells the subroutine what N is, but on return, N is what it was
!        before this call.
!
      DO 217 JZ = 1,REGK
      SUMZ = 0.0D0                                                    
      DO 218 JY = 1,REGN
      TSA = XTR(JZ,JY) * Y(JY)                                          
      SUMZ = SUMZ + TSA                                                 
 218  CONTINUE                                                          
      XTRY(JZ) = SUMZ                                                   
 217  CONTINUE                                                          
      DO 220 LA = 1,REGK
      SUMR = 0.0D0                                                    
      DO 222 LB = 1,REGK
      SSTA = XTRXI(LA,LB) * XTRY(LB)                                    
      SUMR = SUMR + SSTA                                                
 222  CONTINUE                                                          
      BETA(LA) = SUMR                                                   
 220  CONTINUE
SR=zero
DO i=1,REGN
   PV=zero
   DO iw=1,REGK
      PV=PV+beta(iw)*X(i,iw)
   END DO
   RES(i)=Y(i)-PV
   SR=SR+RES(i)*RES(i)
END DO
RESSD=sqrt(sr/(REGN-one))
RETURN; END
